﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using DataAccess;

namespace DataService
{
    public class UsersArticleService
    {
        protected string _connectionString;

        public UsersArticleService(string connectionString)
        {
            _connectionString = connectionString;
        }

        public void CreateItem(UsersArticle item)
        {
            using (var dataContext = new DataContext(_connectionString))
            {
                var itemNew = item;
                var table = dataContext.GetTable<UsersArticle>();
                table.InsertOnSubmit(itemNew);
                dataContext.SubmitChanges();
            }
        }

        public void DeleteItem(UsersArticle item)
        {
            using (var dataContext = new DataContext(_connectionString))
            {
                var table = dataContext.GetTable<UsersArticle>();
                var deletedItem = table.Where(
                    record => record.ArticleID == item.ArticleID && record.UserID == item.UserID).First();
                table.DeleteOnSubmit(deletedItem);
                dataContext.SubmitChanges();
            }
        }

        public void DeleteItemsRelatedToArticle(int articleId)
        {
            using (var dataContext = new DataContext(_connectionString))
            {
                var table = dataContext.GetTable<UsersArticle>();
                var deletedItems = table.Where(
                    record => record.ArticleID == articleId).ToList();
                table.DeleteAllOnSubmit(deletedItems);
                dataContext.SubmitChanges();
            }
        }

    }
}
